package com.zking.myservlet.category;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import com.zking.dao.CategoryDao;
import com.zking.entity.Category;

@WebServlet("/manage/categoryAdd")
public class CategoryAdd extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理请求的编码，解决中文乱码
        request.setCharacterEncoding("utf-8");
        //处理响应的编码 ，解决中文乱码
        response.setContentType("text/html;charset=utf-8");

        System.out.println("CategoryAdd===进来");
        int parentId = Integer.parseInt(request.getParameter("parentId"));
        String name = request.getParameter("className");
        Category category = new Category();
        int count = CategoryDao.getCategoryCount();
        category.setEpc_name(name);
        category.setEpc_id(count+2);
        if(parentId == -1){
            category.setEpc_parent_id(category.getEpc_id());
        }else{
            category.setEpc_parent_id(parentId);
        }
        boolean flag = CategoryDao.addCategory(category);
        if(flag){
            response.sendRedirect("categorySelectAll");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
